package com.cie2.janji.pages.admin;

import org.apache.log4j.Logger;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.cie2.janji.base.CieAdminPage;
import com.cie2.janji.entities.User;
import com.cie2.janji.entities.User.AccessLevel;
import com.cie2.janji.services.UserManagement;


public class CreateUser extends CieAdminPage {

	static Logger logger =
	       Logger.getLogger(CreateUser.class.getName());
			
    private Long id;

	@Property
	private User user;
	
	// for select
	@Persist
	private AccessLevel accessLevel;
	
	private AccessLevel userAccessLevel;
	
	//services
    @Inject
    private UserManagement userManagement;
    
    @CommitAfter
    Object onSuccess()
    {    	
    	if(id != null) {
    		userManagement.updateUser(user, userAccessLevel);
    	}
    	else {
	    	userManagement.addUser(user, userAccessLevel);
    	}
    	
		return Admin.class;
		
    }

    void onActivate(Long id) {
    	this.id = id;
    	user = userManagement.load(id);	
    	accessLevel = user.getCurrentProfile().getAccessLevel();
     }

     Long onPassivate() {
        return id;
     }     
     
     // for select
     public AccessLevel getUsertype() {
    	 return accessLevel;
     }     
     
     public void setUsertype(AccessLevel accessLevel) {
    	 userAccessLevel = accessLevel;
     }
}
